<template>
  <van-icon :color="like === 1 ? 'red' : ''" :name="like === 1?'good-job':'good-job-o'" @click="changeLike" />
</template>

<script>
import { addLike, deleteLike } from '@/api/article.js'
export default {
  data () {
    return {}
  },
  model: {
    prop: 'like',
    event: 'changeCollected'
  },
  props: {
    like: {
      type: Number,
      required: true
    },
    articleId: {
      type: [Number, String, Object],
      required: true
    }
  },
  methods: {
    async changeLike () {
      try {
        this.$toast.loading({
          duration: 0, // 持续展示 toast
          message: '操作中...',
          forbidClick: true // 是否禁止背景点击
        })
        // 如果已经点赞，则取消点赞
        if (this.like === 1) {
          await deleteLike(this.articleId)
          this.like = -1
          this.$toast.success('取消点赞')
        } else {
          // 否则添加点赞
          await addLike(this.articleId)
          this.like = 1
          this.$toast.success('点赞成功')
        }
      } catch (err) {
        console.log(err)
        this.$toast.fail('操作失败')
      }
    }
  },
  computed: {},
  watch: {},
  filters: {},
  components: {},
  created () { },
  mounted () { }
}
</script>

<style scoped lang='less'>
</style>
